﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StyleMVVM.Logging;
using StyleMVVM.Utilities;
using l4n = log4net;

namespace StyleMVVM.log4net
{
	public class Appender : ILogAppender
	{
		public Task<bool> Configure()
		{
			l4n.Config.XmlConfigurator.Configure();

			return TaskHelper.ReturnTask(true);
		}

		public void Flush()
		{
		}

		public LogLevel LoggingLevel { get; set; }

		public void Log(LogEntry entry)
		{
			string loggerName = entry.Supplemental;

			if (loggerName == null)
			{
				loggerName = string.Empty;
			}

			l4n.ILog logger = l4n.LogManager.GetLogger(loggerName);

			switch (entry.Level)
			{
				case LogLevel.DEBUG:
					logger.Debug(entry.Message, entry.Exception);
					break;
				case LogLevel.INFO:
					logger.Info(entry.Message, entry.Exception);
					break;
				case LogLevel.WARN:
					logger.Warn(entry.Message, entry.Exception);
					break;
				case LogLevel.ERROR:
					logger.Error(entry.Message, entry.Exception);
					break;
				case LogLevel.FATAL:
					logger.Fatal(entry.Message, entry.Exception);
					break;
			}
		}
	}
}